package com.emm.yixun.system.service.impl.app;

import com.emm.yixun.basic.service.ResourcesConfigService;
import com.emm.yixun.common.app.GetMerchantResouceReqDto;
import com.emm.yixun.common.app.GetMerchantResouceResDto;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.model.ResourcesConfig;
import com.emm.yixun.common.model.dto.ResourcesConfigDto;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.system.service.app.MerchantResourcesAppFacade;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */
@Service("resourcesConfigAppFacade")
@WebService(name="com.emm.yixun.system.service.app.MerchantResourcesAppFacade")
public class MerchantResourcesAppFacadeImpl implements MerchantResourcesAppFacade {
    private  final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ResourcesConfigService service;
    @Override
    public ResponseList<ResourcesConfig> findByDto(ResourcesConfigDto dto) {
        ResponseList<ResourcesConfig> response=new ResponseList<ResourcesConfig>();
        try {
            if(null==dto){
                response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
                return response;
            }
            List<ResourcesConfig> list=service.selectListByDto(dto);
            if(null==list || list.size() == 0){
                response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
                return response;
            }
            response.success(list);
        } catch (Exception e) {
            response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
            logger.error("查询数据错误：{}",e);
        }
        return response;
    }

    @Override
    public GetMerchantResouceResDto findMerchantResourceByDto(GetMerchantResouceReqDto dto){
        GetMerchantResouceResDto res = new GetMerchantResouceResDto();
        ResourcesConfigDto resourcesConfigDto = new ResourcesConfigDto();
        resourcesConfigDto.setMerchantId(dto.getMerchantId());
        resourcesConfigDto.setUrl(dto.getResourceCode());

        if(StringUtils.isEmpty(dto.getResourceCode())){//如果资源code为空，则查询商家返回商家维度的资源权限
            res.setResult("1");
            res.setNecessary("1");
        }else{
            List list = service.selectListByDto(resourcesConfigDto);
            if(list !=null && list.size() > 0){
                res.setResult("1");
                res.setNecessary("1");
            }else{
                res.setResult("0");
                res.setErrorMsg("未查询到相关资源");
            }
        }
        return res;
    }
}
